<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/3/23
 * Time: 15:23
 *
 * @copyright: ©2024 永州微聚 商网络科技有限公司
 * @link: https://www.weijuYunke.com
 */

namespace app\admin\controller;

use app\common\model\LevelCami;
use app\Request;

class Level
{
    public function levelList(Request $request)
    {
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $levelModel = new \app\common\model\Level();
        $list = $levelModel->limit($start, $limit)->where(['level'=> 1])->select()->toArray();
        $count = $levelModel->count();
        return success('会员列表', ['list' => $list, 'count' => $count]);
    }

    public function addLevel(Request $request)
    {
        $param = $request->param();
        $levelModel = new \app\common\model\Level();
       // $levelInfo = $levelModel->where(['level'=> $param['level']])->find();
      /*  if ($levelInfo){
            return error('等级已存在');
        }*/
        $data = [
            'name' => $param['name'],
            //'desc' => $param['desc'],
            'desc' => $param['desc'],
            'level' => 1,
            'price' => $param['price'],
            'effective_time' => $param['effective_time'],
           // 'icon' =>  $param['icon'],
           /* 'dazi_number' => $param['circle_number'],
            'circle_number' => $param['circle_number'],
            'service_number' => $param['service_number'],
            'community_number' => $param['community_number'],*/
            'create_time' => time(),
            'first_commission' => $param['first_commission'],
            'second_commission' => $param['second_commission']
        ];

        $levelModel->insert($data);
        return success('添加成功');
    }

    public function editLevel(Request $request)
    {
        $param = $request->param();
        $levelModel = new \app\common\model\Level();
        $data = [
            'name' => $param['name'],
            //'desc' => $param['desc'],
            'desc' => $param['desc'],
            'level' => 1,
            'price' => $param['price'],
            'effective_time' => $param['effective_time'],
            'first_commission' => $param['first_commission'],
            'second_commission' => $param['second_commission']
            //'icon' =>  $param['icon'],

        ];
            if (isset($param['status'])) {
                $data['status'] = $param['status'];
            }
            $levelModel->where(['id' => $param['id']])->update($data);


        return success('修改成功');
    }

    public function updateLevelStatus(Request $request)
    {
        $param = $request->param();
        $levelModel = new \app\common\model\Level();

        $data['status'] = $param['status'];

        $levelModel->where(['id' => $param['id']])->update($data);


        return success('修改成功');
    }

    public function addLevelCami(Request $request){
        $param  = $request->param();
        $data =[];
        for ($i=0;$i<= $param['number'];$i++){
            $roundStr  = $this->createNonceStr(12);
            $data[$i] = [
                'round_str' =>$roundStr,
                'level_id' => $param['level_id'],
                'create_time' => time()
            ];
        }
        $levelCamiModel = new LevelCami();
        $levelCamiModel->insertAll($data);
        return success('添加成功');

    }

    public function createNonceStr($length = 16,$data = [])
    {
        $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ23456789';
        $str = '';
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        if (in_array($str,$data)){
            $str = $this->createNonceStr($length);
        }
        $levelCamiModel = new LevelCami();
        $camiInfo = $levelCamiModel->where(['round_str' =>$str ])->find();
        if ($camiInfo){
            $str = $this->createNonceStr($length);
        }
        return $str;
    }

    public function camiList(Request $request){
        $param  = $request->param();
        $levelCamiModel = new LevelCami();
        $where = [];
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }

        if (isset($param['status']) && !empty($param['status'])) {
            $where[] = ['cm.status','=',$param['status']];
        }
        if (isset($param['level_id']) && !empty($param['level_id'])) {
            $where[] = ['cm.level_id','=',$param['level_id']];
        }
        $list = $levelCamiModel->alias('cm')
            ->leftJoin('user u', 'cm.uid = u.id')
            ->leftJoin('level l', 'cm.level_id = l.id')
            ->where($where)
            ->field('cm.*,l.name,l.desc,l.effective_time,u.nickname,u.phone,u.avatar')
            ->limit($start,$limit)
            ->select()->toArray();
        $count = $levelCamiModel->alias('cm')
            ->leftJoin('user u', 'cm.uid = u.id')
            ->leftJoin('level l', 'cm.level_id = l.id')
            ->where($where)
           ->count();
        return success('会员卡密',['list' =>$list,'count' => $count ]);

    }

    public function deleteCami(Request $request){
        $param  = $request->param();
        $levelCamiModel = new LevelCami();
        $camiInfo = $levelCamiModel->where(['id' => $param['id']])->find();
        if ($camiInfo['status'] == 2){
            return error('卡密已使用');
        }
        $levelCamiModel->where(['id' => $param['id']])->delete();
        return success('删除成功');
    }

    public function userLevelOrderList(Request $request){
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $where = [];
        if (isset($param['name']) && !empty($param['name'])) {
            $where[] = ['nickname','like','%'.$param['nickname'].'%'];
        }
        if (isset($param['status']) && !empty($param['status'])) {
            $where[] = ['lo.status','=',$param['status']];
        }
        if (isset($param['start_time']) && !empty($param['start_time'])) {
            $where[] = ['lo.create_time', '>=', strtotime($param['start_time'])];
        }
        if (isset($param['end_time']) && !empty($param['end_time'])) {
            $where[] = ['lo.create_time', '>=', strtotime($param['end_time'])];
        }
        if (isset($param['type']) && !empty($param['type'])) {
            $where[] = ['lo.type','=',$param['type']];
        }
        $levelOrderModel = new \app\common\model\LevelOrder();
        $list = $levelOrderModel ->alias('lo')
            ->leftJoin('user u','lo.uid = u.id')
            ->leftJoin('level l','l.id = lo.level_id')
            ->where($where)
            ->field('lo.*,l.name,l.desc,l.effective_time,l.name,u.nickname,u.avatar')
            ->limit($start, $limit)
            ->select()->toArray();
        $count = $levelOrderModel ->alias('lo')
            ->leftJoin('user u','lo.uid = u.id')
            ->leftJoin('level l','l.id = lo.level_id')
            ->where($where)->count();
        return success('会员购买记录', ['list' => $list, 'count' => $count]);
    }

    public function userLevelList(Request $request){
        $param = $request->param();
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $where =  [];
        if (isset($param['name']) && !empty($param['name'])) {
            $where[] = ['nickname','like','%'.$param['nickname'].'%'];
        }
        if (isset($param['status']) && !empty($param['status'])) {
            $where[] = ['ul.status','=',$param['status']];
        }
        $userLevelModel = new \app\common\model\UserLevel();
        $list = $userLevelModel ->alias('ul')
            ->leftJoin('user u','ul.uid = u.id')
            ->leftJoin('level l','l.id = ul.level_id')
            ->where($where)
            ->field('ul.*,l.name,l.desc,l.effective_time,l.name,u.nickname,u.avatar')
            ->limit($start, $limit)
            ->select()->toArray();
        $count = $userLevelModel ->alias('ul')
            ->leftJoin('user u','ul.uid = u.id')
            ->leftJoin('level l','l.id = ul.level_id')->where($where)->count();
        return success('会员到期记录', ['list' => $list, 'count' => $count]);
    }


}